package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-04-14
 * Time: 18:03
 */
public class DelOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("删除图书");
        System.out.println("请输入要删除的图书的名字");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.nextLine();
        int currenSize=bookList.getUsedSize();
        int index=0;
        int i=0;
        for ( i = 0; i < currenSize; i++) {
            Book book=bookList.getPos(i);
            if(book.getName().equals(name)){
                System.out.println("找到这本书了");
                index=i;
               break;
            }
        }
      if(i==currenSize){
          System.out.println("没有你要删除的图书");
          return;
      }
        for (int j = index; j < currenSize-1; j++) {
            Book book=bookList.getPos(j+1);
            bookList.setBooks(j,book);
        }
        bookList.setBooks(currenSize-1,null);
        bookList.setUsedSize(currenSize-1);
    }
}
